import getUserCard from "../../utils/getUserCard.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    lists: [],
    userid:0,
    context:{},
    IsAgent: 0,//0为分销商，1为代理商
    isShowLevel:0, //是否显示下级
    typeid:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.typeid){
      this.setData({ typeid:options.typeid})
    }
    this.setData({ 
      userid: options.userid, 
      isShowLevel: options.isShowLevel 
    })
    this.GetMyTeam(options.userid, options.isShowLevel);
  },

  //获取我的分销团队
  GetMyTeam: function (userid, isShowLevel) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributorTeam',
      data: {
        userId: userid,
        page: that.data.CurrentPage,
        itemsPerPage: 10,
        nickname: "",
        rescure: false
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.Code == 0) {
          var list = res.data.Data.Items;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.lists
            list = preGoods.concat(list);
          }
         
          that.setData({
            lists: isShowLevel == 1?list:[],
            loadingMoreHidden: res.data.Data.TotalPages,
            IsAgent: res.data.Data.Context.IsAgent,
            context: res.data.Data.Context
          })
          var totalChildCoun = isShowLevel == 1 ?res.data.Data.TotalItems:0;
          var navTitle = "";
          if (that.data.typeid == 2){
            navTitle = "Ta的人脉" + (totalChildCoun > 0 ? "(" + totalChildCoun + ")" : "")
          }else{
            navTitle = "Ta的下级" + (totalChildCoun > 0 ? "(" + totalChildCoun + ")" : "")
          }
          wx.setNavigationBarTitle({
            title: navTitle
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.GetMyTeam(that.data.userid, that.data.isShowLevel);
    } else {
      return
    }
  },

  //查看名片
  toCardDetails: function (e) {
    var userid = e.currentTarget.dataset.id
    getUserCard(userid, function (cardid) {
      wx.reLaunch({
        url: '/pages/home/index?id=' + cardid,
      })
    })
  }

})